<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" :append-to-body="true" width="65%" @open="handleOpen" @close="handleClose" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form ref="mainFrom" :model="mainFrom" label-width="120px" :rules="mainRules"  size="small">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form-item label="参数名称:" prop="characteristicName">
           <el-input v-model="mainFrom.characteristicName"  placeholder="参数名称" class="w-240" maxlength="30"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="参数编号:" prop="characteristicCode">
            <el-input v-model="mainFrom.characteristicCode"  placeholder="参数编号" class="w-240"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form-item label="参数单位:" prop="unit">
           <el-input v-model="mainFrom.unit"  placeholder="参数单位" class="w-240"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="参数描述:">
            <el-input type="textarea"  row="3" v-model="mainFrom.descriptions"  placeholder="参数描述" class="w-240" maxlength="50"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmitData">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {add, update, getDetail} from "@/api/system/featurePool"
export default {
  data() {
    return {
      dialogFormVisible: false,
      mainFrom: {
        id: null,
        characteristicName: '',
        characteristicCode: '',
        unit: '',
        descriptions: ''
      },
      mainRules: {
        characteristicName: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        characteristicCode: [
          { required: true, message: '请输入参数编号', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '请输入参数单位', trigger: 'blur' }
        ]
      },
      submitLoading: false
    }
  },
  props: {
    title: String,
    currentId: String
  },
  methods: {
    // 打开弹窗回调事件
    handleOpen () {
      if (this.currentId) { 
        getDetail(this.currentId).then(res => {
          this.mainFrom = res.data.data
        })
      }
    },
    // 关闭弹窗
    handleClose() {
      this.submitLoading = false
      this.mainFrom = {
        id: null,
        characteristicName: '',
        characteristicCode: '',
        unit: '',
        descriptions: ''
      }
      this.$refs.mainFrom.resetFields()
      this.dialogFormVisible = false
    },
    // 提交
    handleSubmitData() {
      this.submitLoading = true
      this.$refs.mainFrom.validate((valid) => {
          if (valid) {
            if (this.currentId) {
              // 编辑
              this.$set(this.mainFrom, 'id', this.currentId)
              update(this.mainFrom).then(res => {
                this.handleClose()
                this.$emit('getCharacteristicPool')
              })
            } else {
              // 新增
              add(this.mainFrom).then(res => {
                this.handleClose()
                this.$emit('getCharacteristicPool')
              })
            }
          }
      })
    }
  }
};
</script>
  
<style scoped>
.featurePool-container {
  position: relative;
}
.featurePool-wrap {
  width: 240px;
  height: 300px;
  background: #F5F7FA;
  border-radius: 5px;
  position:absolute;
  top: 33px;
  left: 130px;
  z-index: 99999 !important;
}
.featurePool-list {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  height: 250px;
}
.scroll-wrap {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
  overflow-x: hidden;
}
.featurePool-bottom {
  position: absolute;
  bottom: 5px;
  right: 5px;
  text-align: right;
}
.norm-config {
 width: 100%;
}
.config-btn {
  display: block;
}
.config-color {
  color: #409EFF;
  font-size: 14px;
  cursor: pointer;
}
.el-checkbox {
  width: 100%;
}
.w-240 {
  width: 240px;
}
.el-form-item--small .el-form-item__content, .el-form-item--small .el-form-item__label {
  line-height: 20px !important;
}
/* .el-dialog__body {
  height: 400px !important;
  overflow-y: auto;
} */
</style>
  